package optimization01;

import ilog.concert.*;
import ilog.cplex.*;

public class Example {
	public static void main(String[] args) throws IloException{
		IloCplex cplex = new IloCplex();
		
		//variables
		IloNumVar x1 = cplex.numVar(0, 40, IloNumVarType.Float, "x1");
		IloNumVar x2 = cplex.numVar(0, Double.MAX_VALUE, IloNumVarType.Float, "x2");
		IloNumVar x3 = cplex.numVar(0, Double.MAX_VALUE, IloNumVarType.Float, "x3");
		
		//objective
		IloNumExpr obj = cplex.numExpr();
		obj = cplex.sum(obj, cplex.prod(1, x1));
		obj = cplex.sum(obj, cplex.prod(2, x2));
		obj = cplex.sum(obj, cplex.prod(3, x3));
		cplex.addMaximize(obj);
		
		//constraints
		IloLinearNumExpr expr1 = cplex.linearNumExpr();
		expr1.addTerm(-1, x1);
		expr1.addTerm(1, x2);
		expr1.addTerm(1, x3);
		IloRange c1 = cplex.addLe(expr1, 20);
				
		IloLinearNumExpr expr2 = cplex.linearNumExpr();
		expr2.addTerm(1, x1);
		expr2.addTerm(-3, x2);
		expr2.addTerm(1, x3);
		IloRange c2 = cplex.addLe(expr2, 30);
		
		cplex.solve();
		//cplex.exportModel("lpex.lp");
		
		System.out.println(cplex.getStatus());
		System.out.println(cplex.getObjValue());
		
		// optimization variable
		System.out.println(cplex.getValue(x1));
		System.out.println(cplex.getValue(x2));
		System.out.println(cplex.getValue(x3));
		
		//shadow price
		System.out.println(cplex.getDual(c1));
		System.out.println(cplex.getDual(c2));
		
		//slack variable
		System.out.println(cplex.getSlack(c1));
		System.out.println(cplex.getSlack(c2));
		
		cplex.close();
	}
}
